
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;
using System.Xml;

namespace Swaf.Config
{
	/// <summary>
	/// Summary description for XmlTextReaderConfigElement.
	/// </summary>
	public class XmlTextReaderConfigElement : IConfigElement
	{
		protected IDictionary m_context;
		protected XmlTextReader m_xmlReader;
		protected bool m_readThroughChildren;

		public XmlTextReaderConfigElement(IDictionary context, XmlTextReader xmlReader)
		{
			m_context = context;
			m_xmlReader = xmlReader;
			m_readThroughChildren = false;
		}

		public System.Collections.IDictionary Context {get{return m_context;}}
		public XmlTextReader textReader{get{return m_xmlReader;}}
		public bool ReadThroughChildren {get{return m_readThroughChildren;}}
		public XmlElement Element
		{
			get
			{
				XmlDocument doc = new XmlDocument();
				string xml =m_xmlReader.ReadOuterXml();
				doc.LoadXml(xml);
				m_readThroughChildren = true;
				return doc.DocumentElement;
			}
		}

		public string getChildText(string defaultValue)
		{
			string v = m_xmlReader.ReadInnerXml();
			m_readThroughChildren = true;
			if(m_xmlReader.NodeType == XmlNodeType.EndElement)
				m_xmlReader.Read();
			return v==null?defaultValue:v;
		}

		string Swaf.Config.IConfigElement.getChildText()
		{
			return getChildText(null);
		}

		public string getAttribute(string name, string defaultValue)
		{
			string v = m_xmlReader.GetAttribute(name);
			return v==null?defaultValue:v;
		}

		string Swaf.Config.IConfigElement.getAttribute(string name)
		{
			return getAttribute(name, null);
		}

		public void checkIllegalAttributes(string[] legalAttributes)
		{
			if(m_xmlReader.MoveToFirstAttribute())
			{
				do
				{
					string a = m_xmlReader.Name;
					bool found = false;
					for(int p2 = 0; p2 < legalAttributes.Length; ++p2)
						if(legalAttributes[p2].Equals(a))
						{
							found = true;
							break;
						}
					if(!found)
						throw new BadConfigException(String.Format("{0} : {1} : unexpected attribute '{2}'",
							m_context["filename"],m_context["path"], a));
				}
				while(m_xmlReader.MoveToNextAttribute());
			}
			m_xmlReader.MoveToElement();
		}

		public IList checkUnknownAttributes(string[] legalAttributes)
		{
			IList unknowns = new ArrayList();
			if(m_xmlReader.MoveToFirstAttribute())
			{
				do
				{
					string a = m_xmlReader.Name;
					bool found = false;
					for(int p2 = 0; p2 < legalAttributes.Length; ++p2)
						if(legalAttributes[p2].Equals(a))
						{
							found = true;
							break;
						}
					if(!found)
						unknowns.Add(new string[]{a, m_xmlReader.Value});
				}
				while(m_xmlReader.MoveToNextAttribute());
			}
			m_xmlReader.MoveToElement();
			return unknowns;
		}


		public int LineNumber
		{
			get
			{
				return m_xmlReader.LineNumber;
			}
		}
		public int LinePosition
		{
			get
			{
				return m_xmlReader.LinePosition;
			}
		}
	}
}
